<%layout("/eova/layout/default.html",{'title' : '新增' + object.name }){%>

<body>
	<form id="addFormATTACHMENT" method="post" class="form">
		<div class="eova-form">

			<div class="eova-form-field" title="对应档案[file_id]" style="">
				<div class="eova-form-lbl">对应档案aaa</div>
				<div>
					<div class="eova-find" id="file_id" name="file_id" value=""
						code="ATTACHMENT" field="file_id"
						url="/attachment/find?code=ATTACHMENT&field=file_id&multiple=false"
						data-options=" disable : false"></div>
				</div>
			</div>

			<div class="eova-form-field" title="附件名称[attachment_name]" style="">
				<div class="eova-form-lbl red">上传附件</div>

				<div class="eova-file">
					<input type="text" id="attachment_name" name="attachment_name"
						value="" style="width: 158px;"><i class="ei"></i> <input
						type="file" id="attachment_name_file" name="attachment_name_file"
						style="left: 49px; top: 216px; width: 190px; border: 1px solid red; cursor: pointer;">
				</div>


			</div>

			<div class="eova-form-field" title="附件存储路径[path]" style="">
				<div class="eova-form-lbl red">附件存储路径</div>
				<div>
					<div class="eova-text" id="path" name="path"
						value="\upload\20160918" data-options=" disable : false"></div>
				</div>
			</div>
	</form>

	<script>
		//附件上传
		$(function() {

			var $input = $('#attachment_name');

			var htmlOptions = eval('({})');
			if (htmlOptions.disable) {
				// 灰色遮罩实现禁用
				$input.parent().mask();
			}

			// init input file
			$('.eova-file').each(function() {
				var offset = $(this).find('input[type=text]').offset();
				$(this).find('input[type=file]').css({
					left : offset.left - 1,
					top : offset.top - 1,
					width : $(this).width()
				});
			});

			// 异步上传(动态绑定事件)
			$(document).on(
					"change",
					"#attachment_name_file",
					function() {
						var $this = $(this);
						console.log($this);

						//获取path和name
						var name = $this.val();
						/*
						var path = $input.parent().parent().find(
								'input[name=path]').val();
						*/
						var path = $(document).find('input[name=path]').val();
						console.log(path);

						$this.siblings('input').val($this.val());

						$.ajaxFileUpload({
							url : '/upload/attachment?name=' + name + '&filedir=' + path,
							secureuri : false,
							fileElementId : 'attachment_name_file',
							dataType : 'json',
							success : function(data, status) {
								// 将返回数据转换为JSON对象
								json = eval(data);
								var msg = json.msg;
								if (json.success) {
									$input.val(json.fileName);
									console.log("上传成功！");
								} else {
									alert(msg);
								}
							},
							error : function(data, status, e) {
								alert('网络异常，请稍后再试！');
							}
						});
					});
		});
	</script>

	<script>
		$(function() {
			var $form = $('#addFormATTACHMENT');
			$form.validator({
				debug : false,
				stopOnError : true,
				focusInvalid : false,
				showOk : false,
				timely : 0,
				msgMaker : false,
				fields : {
					attachment_name : {
						rule : "附件名称:required;"
					},
					path : {
						rule : "附件存储路径:required;"
					},
				}
			});
			$form.on("validation", $.validation);
		});
	</script>
	<script type="text/javascript">
		var $form = $('#addFormATTACHMENT');

		var submitNow = function($dialog, $grid, $pjq) {
			var url = "/attachment/doAdd/ATTACHMENT";
			$.post(url, sy.serializeObject($form), function(result) {
				if (result.success) {
					//$pjq.messager.alert('提示', result.msg, 'info');
					$.slideMsg("新增档案附件成功！", $pjq);
					if ($grid.context.URL.indexOf('/single_tree') != -1) {
						$grid.treegrid('load');
					} else {
						$grid.datagrid('load');
					}
					$dialog.dialog('destroy');
				} else {
					$.alert($pjq, result.msg);
				}
			}, 'json');
		};
		var submitForm = function($dialog, $grid, $pjq) {
			// nice validator
			$form.isValid(function(isValied) {
				if (isValied) {
					submitNow($dialog, $grid, $pjq);
				}
			});
		};
	</script>
</body>

<%}%>
